﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using He.Common;

namespace He.Web.manage.category
{
    public partial class batch : He.Web.UI.Manage
    {
        public He.Model.channel channel;                     //栏目
        public int channel_id;                                       //栏目ID
        public int parent_id;
        BLL.category bll = new BLL.category();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.QueryString["channel_id"], out this.channel_id))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您的参数不正确。", "back", "Error");
                return;
            }
            channel = new BLL.channel().GetModel(channel_id);
            if (!this.Page.IsPostBack)
            {
                chkLoginLevel("addCategory" + channel.id);

                DataTable dt = this.bll.GetList(0, this.channel_id);
                this.ddlPid.Items.Clear();
                this.ddlPid.Items.Add(new ListItem("作为顶级分类", "0"));
                foreach (DataRow dr in dt.Rows)
                {
                    string id = dr["id"].ToString();
                    int layer = int.Parse(dr["layer"].ToString());
                    string title = dr["title"].ToString().Trim();
                    if (layer < this.channel.max_layer)
                    {
                        title = StringHelper.Repeat("　", layer) + "├ " + title;
                        this.ddlPid.Items.Add(new ListItem(title, id));
                    }
                }
                if (int.TryParse(Request.QueryString["parentId"], out this.parent_id))
                {
                    this.ddlPid.SelectedValue = parent_id.ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int parent_id = int.Parse(this.ddlPid.SelectedValue);
            int sort_id = int.Parse(this.txtSortId.Text.Trim());
            string titles = this.txtTitles.Text.Replace("\r\n", "|").Trim(new char[] { '|' });
            if (!string.IsNullOrEmpty(titles))
            {
                string[] arr = titles.Split(new char[] { '|' });
                foreach (string title in arr)
                {
                    if (!string.IsNullOrEmpty(title.Trim()))
                    {
                        this.AddCategory(this.channel_id, parent_id, title, sort_id);
                    }
                }
            }
            SaveLogs(string.Format("[{0}管理]批量增加分类", channel.manage_title));
            JscriptPrint("添加信息成功啦！", "list.aspx?channel_id=" + channel_id, "Success");
        }
        private void AddCategory(int channelId, int pid, string title, int sortId)
        {
            string id_list = "";
            int layer = 1;
            Model.category model = new Model.category
            {
                channel_id = channelId,
                parent_id = pid,
                title = title,
                sort_id = int.Parse(this.txtSortId.Text.Trim()),
                id_list = id_list,
                seo_keywords = title,
                seo_desc = title,
                add_time = DateTime.Now.ToString()
            };
            int rtnId = this.bll.Add(model);
            if (pid > 0)
            {
                Model.category parentCategroy = this.bll.GetModel(pid);
                if (parentCategroy != null)
                {
                    id_list = parentCategroy.id_list + rtnId + ",";
                    layer = parentCategroy.layer + 1;
                }
            }
            else
            {
                id_list = "," + rtnId + ",";
                layer = 1;
            }
            model.id = rtnId;
            model.id_list = id_list;
            model.layer = layer;
            this.bll.Update(model);
        }
    }
}